<!DOCTYPE html>
<html>
	<head>
	<meta charset="utf-8"/>
	<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
	<title>登陆</title>
	<link rel="stylesheet" type="text/css" href="css/mui.css"/>
	</head>
	<body>
		<div class="mui-content">
		    <button id="login" class="mui-btn mui-btn-block mui-btn-success" onclick="authLogin()">Login</button>
		</div>
		
		<script src="js/mui.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			var auths=null;
			var mainPage = null;
			var userInfo = null;
			
			mui.plusReady(function(){			
				// 扩展API加载完毕，现在可以正常调用扩展API
				plus.oauth.getServices(function(services) {
					auths = services;
					console.log('auths:' + JSON.stringify(auths));
				}, function(e){
					console.log("获取分享服务列表失败："+e.message+" - "+e.code);
				});
				
				mainPage = mui.preload({
					"url": 'main.html',
					"id": 'main'
				});
			})

			// 登录操作
			function authLogin(){
				var s = auths[0];
				if (!s.authResult) {
					s.login(function(e){
						// 获取登录操作结果
						var target = e.target;
						userInfo = target.userInfo;
						// console.log("id:" + JSON.stringify(target.id))
						// console.log("description:" + JSON.stringify(target.description))
						// console.log( "authResult" + JSON.stringify(target.authResult));
						// console.log( "userInfo：" + JSON.stringify(target.userInfo));
						// console.log( "extra：" + JSON.stringify(target.extra));
						goToMain();
					},function(e){
						console.log( "登录认证失败："+e.message+" - "+e.code );
					});
				}else{
					console.log("已经登录认证！");
					s.getUserInfo(function(e){
						// console.log(JSON.stringify(e.target))
						goToMain();
					});
				}
			}
			
			function goToMain(){
				mui.fire(mainPage,'show',{
			        userInfo: userInfo
		      	});
				mui.openWindow({
					"id": 'main'
				});
			}
			
			mui("#login")[0].addEventListener('tap',function(){
				authLogin();
			})
		</script>
	</body>
</html>